<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { auditApi } from '@/api/controller';
  import { Toastloading, ToastSuccess, ToastWarning } from '@/utils/public';
  import Pagination from "@/components/Pagination/index.vue";
  import { ElMessageBox } from 'element-plus';
  const multipleTableRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
    welcomeId:[] as any[],
  })
  const auditOption = [
    { type:1, label:'待审核' },
    { type:2, label:'通过' },
    { type:3, label:'驳回' },
  ]
  const initialQuery = {
    union_id:'',
    room_id:'',
    union_name:'',
    content:'',
    audit_status:1,
    page: 1,
    limit: 20,
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleFilter = ()=>{
    listQuery.page = 1
    getList()
  }
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      ...initialQuery
    })
    getList()
  }
  const auditChange = (e:any = 0)=>{
    if(e != 1) action.welcomeId = []
  }
  const handleSelectionChange = (val=[]) => {
    action.welcomeId = val.map((res:any)=> res.id)
  }
  const oneClickPass = ()=>{
    if(action.welcomeId.length == 0 || !action.welcomeId.length) return ToastWarning('请选择需要批量审核的欢迎语')
    ElMessageBox.confirm(`确定通过选中的所有欢迎语吗`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      auditApi.auditBatchRoomWelcomeWord({
        ids:action.welcomeId.toString()
      }).then(()=>{
        ToastSuccess(`通过成功!`);
        action.welcomeId = []
        multipleTableRef.value.clearSelection()
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const paginationChange = async()=>{
    await getList()
  }
  const handleAudit = (row:any = {},audit_status=0)=>{
    if(!audit_status) return ToastWarning('status Null')
    const { id=0 , room_name=''} = row
    if(!id) return ToastWarning('id Null')
    const text = audit_status==2?'通过':'驳回'
    ElMessageBox.confirm(`${text}房间：${room_name}的欢迎语吗`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      auditApi.auditRoomWelcomeWord({
        id,
        audit_status
      }).then(()=>{
        ToastSuccess(`${text}成功!`);
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await auditApi.listRoomWelcomeWord(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.list = data
    action.total = total
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">公会ID：</div>
          <el-input v-model="listQuery.union_id" placeholder="请输入公会ID"/>
        </div>
        <div class="labelItem">
          <div class="label">房间ID：</div>
          <el-input v-model="listQuery.room_id" placeholder="请输入房间ID"/>
        </div>
        <div class="labelItem">
          <div class="label">公会名称：</div>
          <el-input v-model="listQuery.union_name" placeholder="请输入公会名称"/>
        </div>
        <div class="labelItem">
          <div class="label">内容：</div>
          <el-input v-model="listQuery.content" placeholder="请输入欢迎语内容"/>
        </div>
        <div class="labelItem">
          <div class="label">审核状态：</div>
          <el-select v-model="listQuery.audit_status" @change="auditChange" placeholder="请选择审核状态">
            <el-option v-for="item in auditOption" :label="item.label" :value="item.type" />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleFilter">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button v-if="listQuery.audit_status == 1" type="success" @click="oneClickPass">一键通过</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      @selection-change="handleSelectionChange"
      ref="multipleTableRef"
      row-key="id"
      border>
      <el-table-column type="selection" width="55" :reserve-selection="true"  />
      <el-table-column width="100" prop="id" label="ID" />
      <el-table-column min-width="120" prop="union_id" label="公会ID" />
      <el-table-column min-width="180" prop="union_name" label="公会名称" />
      <el-table-column min-width="120" prop="room_id" label="房间ID" />
      <el-table-column min-width="180" prop="room_name" label="房间名称" />
      <el-table-column min-width="120" prop="audit_status" label="审核状态">
        <template #default="scope">
          {{ auditOption.find(res=>res.type == scope.row.audit_status)?.label || scope.row.audit_status }}
        </template>
      </el-table-column>
      <el-table-column min-width="200" prop="content" label="内容" />
      <el-table-column min-width="120" prop="created_at" label="创建时间" />
      <el-table-column width="180" fixed=right label="操作">
        <template #default="scope">
          <template v-if="scope.row.audit_status == 1">
            <el-button type="primary" @click="handleAudit(scope.row,2)">通过</el-button>
            <el-button type="danger" @click="handleAudit(scope.row,3)">驳回</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="paginationChange" />
  </div>
</template>